package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.Wa_Main_ListBean;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Main_changeWaAdapter extends BaseAdapter {
    List<Wa_Main_ListBean.UserbadyListBean> waBeanList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;

    public Main_changeWaAdapter() {
    }

    public Main_changeWaAdapter(List<Wa_Main_ListBean.UserbadyListBean> waBeanList, Context context) {
        this.waBeanList = waBeanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<Wa_Main_ListBean.UserbadyListBean> waBeanList) {
        this.waBeanList = waBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return waBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return waBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_main_dig_chagewa, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_main_changewa_name = (TextView) convertView.findViewById(R.id.textView_main_changewa_name);
            viewHolder.radioButton_main_changewa_name = (RadioButton) convertView.findViewById(R.id.radioButton_main_changewa_name);
            viewHolder.radioButton_main_changewa = (RadioButton) convertView.findViewById(R.id.radioButton_main_changewa);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Wa_Main_ListBean.UserbadyListBean userbadyListBean = waBeanList.get(position);

        viewHolder.textView_main_changewa_name.setText(userbadyListBean.getNickName());
        viewHolder.radioButton_main_changewa_name.setText(userbadyListBean.getStatus() == 2
                ? R.string.main_chage_yangyu : R.string.main_chage_yunyu);

        if (ForPreference.getString(ForContents.WA_DEFAULT_ID).equals(userbadyListBean.getId() + "")) {
            //预产期/生日
            Date date = new Date();
            date.setTime(Long.parseLong(userbadyListBean.getBirthtime().substring(6, userbadyListBean.getBirthtime().length() - 2)));
            ForPreference.putString(ForContents.WA_BIRTHDAY, new SimpleDateFormat("yyyy-MM-dd").format(date));
        }

        viewHolder.radioButton_main_changewa.setChecked(false);
        viewHolder.radioButton_main_changewa_name.setChecked(false);

        viewHolder.radioButton_main_changewa.setChecked(ForPreference.getString(ForContents.WA_DEFAULT_ID).equals(userbadyListBean.getId() + ""));
        viewHolder.radioButton_main_changewa_name.setChecked(userbadyListBean.getStatus() == 2);

        viewHolder.radioButton_main_changewa.setClickable(false);
        viewHolder.radioButton_main_changewa_name.setClickable(false);
        return convertView;
    }

    public class ViewHolder {
        TextView textView_main_changewa_name;
        RadioButton radioButton_main_changewa_name;
        RadioButton radioButton_main_changewa;
    }
}
